// Created by WXX on 2021/9/22 11:11
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：36 ms, 在所有 C++ 提交中击败了40.35%的用户
 * 内存消耗：18 MB, 在所有 C++ 提交中击败了98.45%的用户
 */
class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>> &a, vector<vector<int>> &b) {
        vector<vector<int>> res;
        for (int i = 0, j = 0; i < a.size() && j < b.size();) {
            if (a[i][1] >= b[j][0] && a[i][0] <= b[j][1])  // 说明a[i]和b[j]有交集
                res.push_back({max(a[i][0], b[j][0]), min(a[i][1], b[j][1])});
            if (a[i][1] <= b[j][1]) i++;
            else j++;
        }
        return res;
    }
};

void OutputBasicArray2D1(vector<vector<int>> nums) {

    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        cout << "[";
        for (int j = 0; j < nums[i].size(); j++) {
            cout << nums[i][j];
            if (j != nums[i].size() - 1) cout << ", ";
        }
        cout << "]";
        if (i != nums.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {

    vector<vector<int>> a = {{0, 2}, {5, 10}, {13, 23}, {24, 25}};
    vector<vector<int>> b = {{1, 5}, {8, 12}, {15, 24}, {25, 26}};
    OutputBasicArray2D1(Solution().intervalIntersection(a, b));

    return 0;
}
